package com.leoman.systemsetting.service.impl;

import com.leoman.common.service.impl.GenericManagerImpl;
import com.leoman.systemsetting.dao.TaskDao;
import com.leoman.systemsetting.entity.Task;
import com.leoman.systemsetting.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by 蔡琦 on 2016/7/1 0001.
 *
 */
@Service
public class TaskServiceImpl extends GenericManagerImpl<Task, TaskDao> implements TaskService{

    @Autowired
    private TaskDao taskDao;

    @Override
    public List<Task> findAll() {
        return taskDao.findAll();
    }

    @Override
    @Transactional
    public void saveTask(Integer[] type, Integer[] num, Integer[] coin, Integer[] type1, Integer[] num1, Integer[] coin1) {

        //修改任务
        List<Task> taskList = taskDao.findAll();
        if (taskList.size() > 0) {
            for (int i = 0; i < type.length; i++) {
                if (type[i] != null) {
                    taskList.get(i).setType(type[i]);
                    if (num[i] != null) {
                        taskList.get(i).setTaskCount(num[i]);
                    }
                    if (coin[i] != null) {
                        taskList.get(i).setCoin(coin[i]);
                    }
                    taskDao.save(taskList.get(i));
                }
            }
        }

        //新增任务
        for (int i = 0; i < type1.length; i++) {
            if (type1[i] != null) {
                Task task = new Task();
                task.setType(type1[i]);
                if (num1[i] != null) {
                    task.setTaskCount(num1[i]);
                }
                if (coin1[i] != null) {
                    task.setCoin(coin1[i]);
                }
                taskDao.save(task);
            }
        }
    }
}
